﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace IntoTheVoid.Units
{

	public class HttpHelper
    {
        private static HttpHelper _instance;
        private static readonly object _lock = new object();

        public static HttpHelper Instance
        {
            get
            {
                if (_instance == null)
                {
					lock (_lock)
                    {
                        if (_instance == null)
                        {
                            return _instance = new HttpHelper();
                        }
                    }
                }
                return _instance;

            }
        }

        public async Task HttpPostHandle<T>(string address, Dictionary<string, object> prameters,
            Action<Exception>onExcption, Action<T> onResoult) where T : class
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            StreamReader streamReader = null;

            try
            {
                string methodConfig = string.Empty;
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(address);

                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 10 * 1000;
				httpWebRequest.Proxy = null;

				if (prameters != null)
                {
                    byte[] prameterBytes = prameters == null ? new byte[0] : Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(prameters));
					httpWebRequest.ContentLength = prameterBytes.Length;
					httpWebRequest.GetRequestStream().Write(prameterBytes, 0, prameterBytes.Length);
                }
                httpWebResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

                streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                string responseContent = await streamReader.ReadToEndAsync();

                if (string.IsNullOrWhiteSpace(responseContent))
                {
                    onResoult(default(T));
                }
                else
                {
                    var resultDto = JsonConvert.DeserializeObject<T>(responseContent);
                    if (resultDto != null)
                    {
                        onResoult(resultDto);
                    }
                }
            }
            catch (Exception ex)
            {
                onExcption.Invoke(ex);
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                }

                if (httpWebResponse != null)
                {
					httpWebResponse.Close();
                }

                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
            }
        }
    }
}
